<template>
  <div>
    <el-form ref="form" :model="user" label-width="80px">
      <el-form-item label="用户名">
        <el-input v-model="user.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="user.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login">登录</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
import axios from 'axios'
export default {
  data () {
    return {
      user: {
        username: '',
        password: ''
      }
    }
  },
  methods: {
    login () {
      const {username, password} = this.user
      axios.get('http://localhost:3000/login', {
        params: {
          username, password
        }
      }).then(res => {
        if (res.data.code === 200) {
          this.$message({
            message: '登录成功',
            type: 'success'
          })
          sessionStorage.setItem('user', JSON.stringify(res.data.user))
          const path = this.$route.query.returnURL
          this.$router.push(path)
        } else {
          this.$message({
            message: res.data.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>

<style>

</style>
